#include "seatdetection.h"
#include <QMessageBox>
#include <QTextStream>
#include <QDomDocument>
#include <QDomProcessingInstruction>

SeatDetection::SeatDetection() {}

SeatDetection& SeatDetection::operator= (SeatDetection& a){
    m_objBaseInfo = a.m_objBaseInfo;
    m_objImageInfo = a.m_objImageInfo;
    m_objCnnInfo = a.m_objCnnInfo;
    m_objDetectionWindowInfo = a.m_objDetectionWindowInfo;

    m_vecCameraInfos.clear();
    for(int i = 0; i < a.GetCameraInfos().count();i++){
        CameraInfo* pCameraInfo = new CameraInfo();
        (*pCameraInfo) = a.GetCameraInfos().at(i);
        m_vecCameraInfos.append(*pCameraInfo);
    }

    m_vecCategoryInfos.clear();
    for(int j = 0; j < a.GetCategoryInfos().count();j++){
        CategoryInfo* pCategoryInfo = new CategoryInfo();
        (*pCategoryInfo) = a.GetCategoryInfos().at(j);
        m_vecCategoryInfos.append(*pCategoryInfo);
    }

    m_objDescription = a.GetDescription();

    return *this;
}

BaseInfo&               SeatDetection::GetBaseInfo(){return m_objBaseInfo;}
ImageInfo&              SeatDetection::GetImageInfo(){return m_objImageInfo;}
CnnInfo&                SeatDetection::GetCnnInfo(){return m_objCnnInfo;}
DetectionWindowInfo&    SeatDetection::GetDetectionWindowInfo(){return m_objDetectionWindowInfo;}
Description&            SeatDetection::GetDescription(){return m_objDescription;}

QVector<CameraInfo>&    SeatDetection::GetCameraInfos(){
    return  m_vecCameraInfos;
}

QVector<CategoryInfo>&  SeatDetection::GetCategoryInfos(){
    return m_vecCategoryInfos;
}

bool SeatDetection::writeXML(const QString &filePath){
    QFile file(filePath);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
        qWarning() << "Unable to open file:" << filePath;
        return false;
    }

    QXmlStreamWriter xmlWriter(&file);
    xmlWriter.setAutoFormatting(true);

    xmlWriter.writeStartDocument();
    xmlWriter.writeStartElement("SeatDetection");

    m_objBaseInfo.WriteXML(xmlWriter);
    m_objImageInfo.WriteXML(xmlWriter);
    m_objCnnInfo.WriteXML(xmlWriter);
    WriteCamerasXML(xmlWriter);
    WriteCategoriesXML(xmlWriter);
    m_objDetectionWindowInfo.WriteXML(xmlWriter);
    m_objDescription.writeXML(xmlWriter);

    //////////////////////////////////////////////////////////////
    /// 开始写入内容 devices
    /*xmlWriter.writeStartElement("Devices");
    foreach (Device *pDevice, m_pDevices) {
        pDevice->writeXML(xmlWriter);
    }
    xmlWriter.writeEndElement();*/
    //////////////////////////////////////////////////////////////
    /// 开始写入内容 steps
    /*xmlWriter.writeStartElement("Steps");
    foreach (Step *pStep, m_pSteps) {
        pStep->writeXML(xmlWriter);
    }
    xmlWriter.writeEndElement();*/
    /////////////////////////////////////////////////////////////
    xmlWriter.writeEndElement();
    xmlWriter.writeEndDocument();

    file.close();
    return true;
}

bool SeatDetection::ReadXML(const QString &filePath){
    QFile file(filePath);

    if (!file.open(QIODevice::ReadOnly)) {
        return false;
    }

    QXmlStreamReader xmlReader(&file);

    QXmlStreamReader::TokenType token = xmlReader.readNext();
    while (!(xmlReader.tokenType() == QXmlStreamReader::EndElement &&
             xmlReader.name() == QStringLiteral("SeatDetection"))) {

        m_objBaseInfo.ReadXML(xmlReader);        

        m_objImageInfo.ReadXML(xmlReader);
        m_objCnnInfo.ReadXML(xmlReader);
        ReadCamerasXML(xmlReader);
        ReadCategoriesXML(xmlReader);
        m_objDetectionWindowInfo.ReadXML(xmlReader);
        m_objDescription.ReadXML(xmlReader);

        if(xmlReader.tokenType() == QXmlStreamReader::EndElement && xmlReader.name() == QStringLiteral("SeatDetection")){
            break;
        }
        xmlReader.readNext();
    }

    file.close();

    return true;
}

bool SeatDetection::ReadCamerasXML(QXmlStreamReader &xmlReader){
    m_vecCameraInfos.clear();

    QString className = xmlReader.name().toString();

    if (xmlReader.isStartElement() && xmlReader.name() == QStringLiteral("Cameras")) {

        while (!(xmlReader.tokenType() == QXmlStreamReader::EndElement
                 && xmlReader.name() == QStringLiteral("Cameras"))) {
            xmlReader.readNext();

            if (xmlReader.isStartElement()) {
                QString className = xmlReader.name().toString();

                CameraInfo* pCameraInfo = new CameraInfo();

                pCameraInfo->ReadXML(xmlReader);

                m_vecCameraInfos.push_back(*pCameraInfo);
            }
        }
    }
    // 这个地方怎么处理
    xmlReader.readNext();
    xmlReader.readNext();
    return true;
}

bool SeatDetection::ReadCategoriesXML(QXmlStreamReader &xmlReader){
    m_vecCategoryInfos.clear();

    QString className = xmlReader.name().toString();

    if (xmlReader.isStartElement() && xmlReader.name() == QStringLiteral("Categories")) {

        while (!(xmlReader.tokenType() == QXmlStreamReader::EndElement
                 && xmlReader.name() == QStringLiteral("Categories"))) {
            xmlReader.readNext();

            if (xmlReader.isStartElement()) {
                QString className = xmlReader.name().toString();

                CategoryInfo* pCategoryInfo = new CategoryInfo();

                pCategoryInfo->ReadXML(xmlReader);
                m_vecCategoryInfos.push_back(*pCategoryInfo);
            }
        }
    }
    // 这个地方怎么处理
    xmlReader.readNext();
    xmlReader.readNext();
    return true;
}

bool SeatDetection::WriteCamerasXML(QXmlStreamWriter &writer){
    writer.writeStartElement("Cameras");
    for(int i = 0; i < m_vecCameraInfos.size(); i++){
        CameraInfo cameraInfo = m_vecCameraInfos[i];
        cameraInfo.WriteXML(writer);
    }
    writer.writeEndElement();

    return true;
}

bool SeatDetection::WriteCategoriesXML(QXmlStreamWriter &writer){
    writer.writeStartElement("Categories");
    for(int i = 0; i < m_vecCategoryInfos.size(); i++){
        CategoryInfo categoryInfo = m_vecCategoryInfos[i];
        categoryInfo.WriteXML(writer);
    }
    writer.writeEndElement();
    return true;
}
